/**
 * gislin 2017年7月13日
 */
package com.btcode.db.wherefilter;

import com.btcode.db.sqlparameter.SQLParameterList;

/**
 * sql where 操作
 *
 * @author gislin 2017年7月13日
 */
public class WhereFilterSingle implements IWhereFilter {

    private WhereFilterType operationType;

    private Object value;

    private String tableName;

    private String columnName;

    public WhereFilterSingle(String tableName, String columnName, WhereFilterType operationType,
        Object value) {
        this.operationType = operationType;
        this.value = value;
        this.tableName = tableName;
        this.columnName = columnName;
    }


    public WhereFilterType getOperationType() {
        return operationType;
    }

    public void setOperationType(WhereFilterType operationType) {
        this.operationType = operationType;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    /**
     * 获取在sql语句中用于占位的名字 gislin 2017年7月17日
     */
    public String getParameterName() {
        return (tableName + "_" + columnName + "_" + operationType.toString()).toUpperCase();
    }

    @Override
    public String getWhereText() {

        String whereText;

        if (operationType == WhereFilterType.LIKE_LEFT || operationType == WhereFilterType.LIKE_RIGHT
            || operationType == WhereFilterType.LIKE_BOTH) {
            whereText =
                " " + getTableName() + "." + getColumnName() + " LIKE @" + getParameterName() + " ";
        }
        else if (operationType == WhereFilterType.GREATER_EQUAL_THAN) {
            whereText =
                " " + getTableName() + "." + getColumnName() + " >= @" + getParameterName() + " ";
        }
        else if (operationType == WhereFilterType.GREATER_THAN) {
            whereText =
                " " + getTableName() + "." + getColumnName() + " > @" + getParameterName() + " ";
        }
        else if (operationType == WhereFilterType.LESS_EQUAL_THAN) {
            whereText =
                " " + getTableName() + "." + getColumnName() + " <= @" + getParameterName() + " ";
        }
        else if (operationType == WhereFilterType.LESS_THAN) {
            whereText =
                " " + getTableName() + "." + getColumnName() + " < @" + getParameterName() + " ";
        }
        else {
            whereText =
                " " + getTableName() + "." + getColumnName() + " = @" + getParameterName() + " ";
        }

        return whereText.toUpperCase();
    }

    @Override
    public SQLParameterList getParameterList() {
        SQLParameterList parameters = new SQLParameterList();

        if (operationType == WhereFilterType.LIKE_LEFT) {
            parameters.put(getParameterName(), "%" + getValue());
        }
        else if (operationType == WhereFilterType.LIKE_RIGHT) {
            parameters.put(getParameterName(), getValue() + "%");
        }
        else if (operationType == WhereFilterType.LIKE_BOTH) {
            parameters.put(getParameterName(), "%" + getValue() + "%");
        }
        else {
            parameters.put(getParameterName(), getValue());
        }

        return parameters;
    }

}
